package com.wanghlme.bookstore.controller.admin;

import com.alibaba.fastjson.JSON;
import com.wanghlme.bookstore.entity.Trade;
import com.wanghlme.bookstore.service.TradeService;
import com.wanghlme.bookstore.service.TradeitemService;

import static com.wanghlme.bookstore.util.OperationStatusEnum.*;

import com.wanghlme.bookstore.util.common.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/order")
@Controller("AdminOrdersController")
public class OrdersController {

    @Autowired
    private TradeService tradeService;

    @Autowired
    private TradeitemService tradeitemService;

    @RequestMapping(value = "/listAll", method = RequestMethod.POST)
    @ResponseBody
    public String listAll() {
        List<Trade> list = tradeService.list();
        return JSON.toJSONString(list);
    }

    @RequestMapping(value = "/showDetail", method = RequestMethod.POST)
    @ResponseBody
    public String showDetail(@RequestParam("orderId") String orderId) {
        if (StringUtil.isNullEmptyBlank(orderId)) {
            return NULL.getValue();
        }
        return JSON.toJSONString(tradeitemService.getTradeItemByTradeId(orderId));
    }

    @RequestMapping(value = "/confirmOrder", method = RequestMethod.POST)
    @ResponseBody
    public String confirmOrder(@RequestParam("orderId") String orderId) {
        if (StringUtil.isNullEmptyBlank(orderId)) {
            return NULL.getValue();
        }
        return tradeService.updateTradeStatus(orderId, 1) ? SUCCESS.getValue() : FAILURE.getValue();
    }
}
